﻿Imports System.Data.SqlServerCe
Imports System.Data
Imports System.Collections.ObjectModel

Namespace SQLCompactDataAccess

    ''' <summary>
    ''' This class provides simple methods to load business entity object(s) and to run the ExecuteNonQuery method.
    ''' </summary>
    ''' <typeparam name="T">Type of entity object to load.</typeparam>
    Public Class EntityLoader(Of T)

#Region " Constructors "

        Private _intAffectedRows As Integer = -1
        Private _objDataAccess As DataAccess

#End Region

#Region " Properties "

        ''' <summary>
        ''' Gets the number of rows affected by the data operation.
        ''' </summary>
        ''' <returns>Integer</returns>
        Public ReadOnly Property AffectedRows() As Integer
            Get
                Return _intAffectedRows
            End Get
        End Property

#End Region

#Region " Constructors "

        Public Sub New(ByVal strConnectionString As String)
            _objDataAccess = New DataAccess(strConnectionString)
        End Sub

        Public Sub New(ByVal objSQLCeConnection As SqlCeConnection)
            _objDataAccess = New DataAccess(objSQLCeConnection)
        End Sub

#End Region

#Region " Methods "

        Public Function LoadList(ByVal CommandText As String, ByVal CommandBehavior As System.Data.CommandBehavior, ByVal ParamArray Parameters() As SqlCeParameter) As List(Of T)

            Dim objResult As New List(Of T)
            Using reader As SqlCeDataReader = _objDataAccess.GetDataReader(CommandText, CommandBehavior, Parameters)

                Dim objBuilder As New ReflectionBuilder(Of T)

                While reader.Read
                    objResult.Add(objBuilder.Build(reader))
                End While

            End Using
            _intAffectedRows = objResult.Count
            Return objResult
        End Function

        Public Function LoadObservableColleciton(ByVal CommandText As String, ByVal CommandBehavior As System.Data.CommandBehavior, ByVal ParamArray Parameters() As SqlCeParameter) As ObservableCollection(Of T)

            Dim objResult As New ObservableCollection(Of T)
            Using reader As SqlCeDataReader = _objDataAccess.GetDataReader(CommandText, CommandBehavior, Parameters)

                Dim objBuilder As New ReflectionBuilder(Of T)

                While reader.Read
                    objResult.Add(objBuilder.Build(reader))
                End While

            End Using
            _intAffectedRows = objResult.Count
            Return objResult
        End Function

        Public Function LoadOne(ByVal CommandText As String, ByVal CommandBehavior As System.Data.CommandBehavior, ByVal ParamArray Parameters() As SqlCeParameter) As T
            _intAffectedRows = 0
            Using reader As SqlCeDataReader = _objDataAccess.GetDataReader(CommandText, CommandBehavior, Parameters)

                Dim objBuilder As New ReflectionBuilder(Of T)

                While reader.Read
                    _intAffectedRows += 1
                    Return objBuilder.Build(reader)
                End While

            End Using
            Return Nothing
        End Function

#End Region

    End Class

End Namespace
